package com.nt.oam.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nt.common.core.domain.entity.SysRole;
import com.nt.common.enums.YesOrNoEnum;
import com.nt.common.utils.DateUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.nt.common.utils.SecurityUtils;
import com.nt.common.utils.StringUtils;
import com.nt.oam.domain.AntAccessDeviceAsset;
import com.nt.oam.vo.project.AssetsNameListVo;
import com.nt.system.service.impl.SysRoleServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nt.oam.mapper.AntAssetCorrelationMapper;
import com.nt.oam.domain.AntAssetCorrelation;
import com.nt.oam.service.IAntAssetCorrelationService;

/**
 * 资产关联Service业务层处理
 *
 * @author JZFeng
 * @date 2025-02-05
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AntAssetCorrelationServiceImpl extends ServiceImpl<AntAssetCorrelationMapper, AntAssetCorrelation> implements IAntAssetCorrelationService {
    private final AntAssetCorrelationMapper antAssetCorrelationMapper;
    private final SysRoleServiceImpl sysRoleService;

    /**
     * 查询资产关联
     *
     * @param assetId 资产关联主键
     * @return 资产关联
     */
    @Override
    public AntAssetCorrelation selectAntAssetCorrelationByAssetId(Long assetId) {
        return antAssetCorrelationMapper.selectAntAssetCorrelationByAssetId(assetId);
    }

    /**
     * 查询资产关联列表
     *
     * @param antAssetCorrelation 资产关联
     * @return 资产关联
     */
    @Override
    public List<AntAssetCorrelation> selectAntAssetCorrelationList(AntAssetCorrelation antAssetCorrelation) {
        return antAssetCorrelationMapper.selectAntAssetCorrelationList(antAssetCorrelation);
    }

    /**
     * 新增资产关联
     *
     * @param antAssetCorrelation 资产关联
     * @return 结果
     */
    @Override
    public int insertAntAssetCorrelation(AntAssetCorrelation antAssetCorrelation) {
        antAssetCorrelation.setCreateTime(DateUtils.getNowDate());
        antAssetCorrelation.setHospitalId((long) Integer.parseInt(antAssetCorrelation.getHospitalName()));
        antAssetCorrelation.setProjectId((long) Integer.parseInt(antAssetCorrelation.getProjectName()));
        // 创建人
        Long userId = (long) Math.toIntExact(SecurityUtils.getUserId());
        SysRole sysRole = sysRoleService.selectRoleById(userId);
        if (Objects.nonNull(sysRole) && StringUtils.isNotEmpty(sysRole.getRoleKey())) {
            antAssetCorrelation.setCreatePerson(sysRole.getRoleKey());
        }
        return antAssetCorrelationMapper.insertAntAssetCorrelation(antAssetCorrelation);
    }

    /**
     * 修改资产关联
     *
     * @param antAssetCorrelation 资产关联
     * @return 结果
     */
    @Override
    public int updateAntAssetCorrelation(AntAssetCorrelation antAssetCorrelation) {
        antAssetCorrelation.setUpdateTime(DateUtils.getNowDate());
        antAssetCorrelation.setHospitalId((long) Integer.parseInt(antAssetCorrelation.getHospitalName()));
        antAssetCorrelation.setProjectId((long) Integer.parseInt(antAssetCorrelation.getProjectName()));
        // 更新人
        Long userId = (long) Math.toIntExact(SecurityUtils.getUserId());
        SysRole sysRole = sysRoleService.selectRoleById(userId);
        if (Objects.nonNull(sysRole) && StringUtils.isNotEmpty(sysRole.getRoleKey())) {
            antAssetCorrelation.setUpdatePerson(sysRole.getRoleKey());
        }
        return antAssetCorrelationMapper.updateAntAssetCorrelation(antAssetCorrelation);
    }

    /**
     * 批量删除资产关联
     *
     * @param assetIds 需要删除的资产关联主键
     * @return 结果
     */
    @Override
    public int deleteAntAssetCorrelationByAssetIds(Long[] assetIds) {
        List<Long> assetIdsList = Arrays.asList(assetIds);
        // 状态删除
        assetIdsList.forEach(a -> {
            AntAssetCorrelation antAssetCorrelation = antAssetCorrelationMapper.selectAntAssetCorrelationByAssetId(a);
            antAssetCorrelation.setIsDeleted(YesOrNoEnum.YES.getCode());
            antAssetCorrelationMapper.updateAntAssetCorrelation(antAssetCorrelation);
        });
        // antAssetCorrelationMapper.deleteAntAssetCorrelationByAssetIds(assetIds)
        return YesOrNoEnum.YES.getCode();
    }

    /**
     * 删除资产关联信息
     *
     * @param assetId 资产关联主键
     * @return 结果
     */
    @Override
    public int deleteAntAssetCorrelationByAssetId(Long assetId) {
        return antAssetCorrelationMapper.deleteAntAssetCorrelationByAssetId(assetId);
    }

    /**
     * 查询资产名称列表
     */
    @Override
    public List<AssetsNameListVo> getAssetsNameList(String assetsName) {
        return antAssetCorrelationMapper.selectAssetsNameList(assetsName);
    }
}
